package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.education.platform.common.Result;
import com.education.platform.entity.SystemConfig;
import com.education.platform.service.SystemConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统配置控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "系统配置", description = "系统配置相关接口")
@RestController
@RequestMapping("/api/system-config")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;

    @Operation(summary = "创建配置", description = "创建系统配置（管理员）")
    @PostMapping
    @SaCheckRole("ADMIN")
    public Result<Long> createConfig(@Validated @RequestBody SystemConfig systemConfig) {
        Long configId = systemConfigService.createConfig(systemConfig);
        return Result.success(configId);
    }

    @Operation(summary = "更新配置", description = "更新系统配置（管理员）")
    @PutMapping("/{id}")
    @SaCheckRole("ADMIN")
    public Result<Void> updateConfig(@PathVariable Long id, @Validated @RequestBody SystemConfig systemConfig) {
        systemConfigService.updateConfig(id, systemConfig);
        return Result.success();
    }

    @Operation(summary = "删除配置", description = "删除系统配置（管理员）")
    @DeleteMapping("/{id}")
    @SaCheckRole("ADMIN")
    public Result<Void> deleteConfig(@PathVariable Long id) {
        systemConfigService.deleteConfig(id);
        return Result.success();
    }

    @Operation(summary = "获取配置详情", description = "根据ID获取配置详细信息（管理员）")
    @GetMapping("/{id}")
    @SaCheckRole("ADMIN")
    public Result<SystemConfig> getConfigDetail(@PathVariable Long id) {
        SystemConfig config = systemConfigService.getConfigById(id);
        return Result.success(config);
    }

    @Operation(summary = "根据键获取配置", description = "根据配置键获取配置信息（管理员）")
    @GetMapping("/key/{configKey}")
    @SaCheckRole("ADMIN")
    public Result<SystemConfig> getConfigByKey(@PathVariable String configKey) {
        SystemConfig config = systemConfigService.getConfigByKey(configKey);
        return Result.success(config);
    }

    @Operation(summary = "根据键获取配置值", description = "根据配置键获取配置值（管理员）")
    @GetMapping("/value/{configKey}")
    @SaCheckRole("ADMIN")
    public Result<String> getConfigValue(@PathVariable String configKey) {
        String value = systemConfigService.getConfigValue(configKey);
        return Result.success(value);
    }

    @Operation(summary = "设置配置值", description = "设置配置值（管理员）")
    @PostMapping("/value")
    @SaCheckRole("ADMIN")
    public Result<Void> setConfigValue(@RequestParam String configKey, @RequestParam String configValue) {
        systemConfigService.setConfigValue(configKey, configValue);
        return Result.success();
    }

    @Operation(summary = "获取所有配置", description = "获取所有系统配置（管理员）")
    @GetMapping("/all")
    @SaCheckRole("ADMIN")
    public Result<List<SystemConfig>> getAllConfigs() {
        List<SystemConfig> configs = systemConfigService.getAllConfigs();
        return Result.success(configs);
    }

    @Operation(summary = "获取公开配置", description = "获取所有公开配置")
    @GetMapping("/public")
    public Result<Map<String, String>> getPublicConfigs() {
        Map<String, String> configMap = systemConfigService.getPublicConfigMap();
        return Result.success(configMap);
    }

    @Operation(summary = "根据类型获取配置", description = "根据配置类型获取配置列表（管理员）")
    @GetMapping("/type/{configType}")
    @SaCheckRole("ADMIN")
    public Result<List<SystemConfig>> getConfigsByType(@PathVariable String configType) {
        List<SystemConfig> configs = systemConfigService.getConfigsByType(configType);
        return Result.success(configs);
    }

    @Operation(summary = "批量更新配置", description = "批量更新系统配置（管理员）")
    @PostMapping("/batch")
    @SaCheckRole("ADMIN")
    public Result<Void> batchUpdateConfigs(@RequestBody Map<String, String> configMap) {
        systemConfigService.batchUpdateConfigs(configMap);
        return Result.success();
    }
}

